﻿using System.Data;
using System.Data.SQLite;
using Framework;

namespace Enterprise
{
    /// <summary>
    /// 管理员服务
    /// </summary>
    public class AdminService : BaseService
    {
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="oldPassword">老密码</param>
        /// <param name="newsPassword">新密码</param>
        /// <returns>返回受影响行数</returns>
        public int ModifyPassword(int id, string oldPassword, string newsPassword)
        {
            Check.Argument.IsNotOutOfLength(oldPassword, 5, 20, "原密码");
            Check.Argument.IsNotOutOfLength(newsPassword, 5, 20, "新密码");
            var parameters = new[]
                                 {
                                     new SQLiteParameter("@id", DbType.Int32),
                                     new SQLiteParameter("@password", DbType.String, 255),
                                     new SQLiteParameter("@newpassword", DbType.String, 255)
                                 };
            parameters[0].Value = id;
            parameters[1].Value = oldPassword;
            parameters[2].Value = newsPassword;
            var count = DbHelperSqLite.ExecuteSql("update tbadmin set copassword=@newpassword where coadminid=@id and copassword=@password", parameters);
            return count;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <param name="username">用户名 </param>
        /// <param name="oldPassword">老密码</param>
        /// <param name="newsPassword">新密码</param>
        /// <returns>返回受影响行数</returns>
        public bool ModifyPassword(int id, string username, string oldPassword, string newsPassword)
        {
            Check.Argument.IsNotOutOfLength(oldPassword, 5, 20, "原密码");
            Check.Argument.IsNotOutOfLength(newsPassword, 5, 20, "新密码");
            var parameters = new[]
                                 {
                                     new SQLiteParameter("@id", DbType.Int32),
                                     new SQLiteParameter("@username", DbType.String, 255),
                                     new SQLiteParameter("@password", DbType.String, 255),
                                     new SQLiteParameter("@newpassword", DbType.String, 255)
                                 };
            parameters[0].Value = id;
            parameters[1].Value = username;
            parameters[2].Value = oldPassword;
            parameters[3].Value = newsPassword;
            var count = DbHelperSqLite.ExecuteSql("update tbadmin set copassword=@newpassword where cousername=@username and coadminid=@id and copassword=@password", parameters);
            if (count > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <returns>Table 返回一张表格</returns>
        public Table GetTable()
        {
            var table = new Table
                          {
                              Rows = DbHelperSqLite.Query("select * from tbadmin order by coadminid").Tables["ds"].Rows,
                              Total = DbHelperSqLite.GetSingle("select count(*) from tbadmin").SafeToInt()
                          };
            return table;
        }
        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="id">管理员ID</param>
        /// <returns>DataRow 返回一条记录</returns>
        public DataRow GetById(int id)
        {

            return DbHelperSqLite.Query("select * from tbadmin where coadminId={0}".FormatWith(id)).Tables["ds"].Rows[0];
        }
        /// <summary>
        /// 用户登入
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool Login(string username, string password)
        {
            var parameters = new[] { new SQLiteParameter("@username", DbType.String, 255),
                                     new SQLiteParameter("@password", DbType.String, 255) };
            parameters[0].Value = username;
            parameters[1].Value = password;
            using (var sdr = DbHelperSqLite.ExecuteReader("select * from tbadmin where cousername=@username", parameters))
            {
                if (sdr.Read())
                {
                    if (sdr["CoPassword"].SafeToString() == password)
                    {
                        return true;
                    }
                    return false;
                }
            }
            return false;
        }
    }
}
